package com.fuxi.cloud.desreport.b;

import com.alibaba.fastjson.JSONObject;
import com.fuxi.cloud.api.data.IDataSetFactory;
import com.fuxi.cloud.common.constant.JmConst;
import com.fuxi.cloud.common.expetion.JimuReportException;
import com.fuxi.cloud.common.util.JimuSpringContextUtils;
import com.fuxi.cloud.common.util.SqlInjectionUtil;
import com.fuxi.cloud.common.util.oConvertUtils;
import com.fuxi.cloud.config.client.JmReportTokenClient;
import com.fuxi.cloud.desreport.entity.JmReportDbField;
import com.fuxi.cloud.desreport.model.JmPage;
import com.fuxi.cloud.dyndb.query.QueryGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * a
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-05-03
 */
public class c {
    private static final Logger e = LoggerFactory.getLogger(c.class);
    private static JmReportTokenClient f;
    public static final String a = " where ";
    public static final String b = " and ";
    public static final String c = " or ";
    public static final String d = "0";

    public c() {
    }

    public static boolean a(List var0) {
        return var0 != null && var0.size() > 0;
    }

    public static List<Map<String, Object>> b(List<Map<String, Object>> var0) {
        ArrayList var1 = new ArrayList();
        Iterator var2 = var0.iterator();

        while(true) {
            Map var3;
            do {
                if (!var2.hasNext()) {
                    return var1;
                }

                var3 = (Map)var2.next();
            } while(var3 == null);

            LinkedHashMap var4 = new LinkedHashMap();
            Set var5 = var3.keySet();
            Iterator var6 = var5.iterator();

            while(var6.hasNext()) {
                String var7 = (String)var6.next();
                Object var8 = var3.get(var7);
                if (var8 instanceof Clob) {
                    var8 = a((Clob)var8);
                } else if (var8 instanceof byte[]) {
                    var8 = new String((byte[])((byte[])var8));
                } else if (var8 instanceof Blob) {
                    try {
                        if (var8 != null) {
                            Blob var9 = (Blob)var8;
                            var8 = new String(var9.getBytes(1L, (int)var9.length()), "UTF-8");
                        }
                    } catch (Exception var10) {
                        var10.printStackTrace();
                    }
                }

                String var11 = var7.toLowerCase();
                var4.put(var11, var8);
            }

            var1.add(var4);
        }
    }

    public static String a(Clob var0) {
        String var1 = "";

        try {
            Reader var2 = var0.getCharacterStream();
            char[] var3 = new char[(int)var0.length()];
            var2.read(var3);
            var1 = new String(var3);
            var2.close();
        } catch (IOException var4) {
            var4.printStackTrace();
        } catch (SQLException var5) {
            var5.printStackTrace();
        }

        return var1;
    }

    private static void a(String var0, String var1, String var2, StringBuffer var3) {
        if ("number".equals(var0)) {
            var3.append(var1);
        } else if ("ORACLE".equals(var2) && var0.toLowerCase().indexOf("date") >= 0) {
            var3.append(com.fuxi.cloud.desreport.b.d.a(var1));
        } else {
            var3.append("'" + var1 + "'");
        }

    }

    public static void a(String var0, String var1, JmReportDbField var2, JSONObject var3, StringBuffer var4) {
        String var5 = var2.getFieldName();
        String var6 = var0 + "__" + var5;
        String var7 = var2.getWidgetType();
        Object var8;
        String var9;
        if (JmConst.QUERY_MODE_RANGE.equals(var2.getSearchMode())) {
            var8 = var3.get(var6 + "_begin");
            if (var8 != null) {
                var9 = var8.toString();
                if (!"".equals(var9)) {
                    var4.append(" AND " + var5 + ">=");
                    a(var7, var9, var1, var4);
                }
            }

            Object var12 = var3.get(var6 + "_end");
            if (var12 != null) {
                String var10 = var12.toString();
                if (!"".equals(var10)) {
                    var4.append(" AND " + var5 + "<=");
                    a(var7, var10, var1, var4);
                }
            }
        } else {
            var8 = var3.get(var6);
            if (var8 != null) {
                var9 = var8.toString();
                if (!"".equals(var9)) {
                    var4.append(" AND ");
                    if ("ORACLE".equals(var1) && var7.toLowerCase().indexOf("date") >= 0) {
                        var4.append(var5 + "=" + com.fuxi.cloud.desreport.b.d.a(var9));
                    } else {
                        boolean var13 = !"number".equals(var7);
                        String var11 = QueryGenerator.getSingleQueryConditionSql(var5, "", var8, var13);
                        var4.append(var11);
                    }
                }
            }
        }

    }

    public static String a(String var0, Map<String, Object> var1) {
        if (var0 != null && !"".equals(var0)) {
            if (var0.endsWith(";")) {
                var0 = var0.substring(0, var0.length() - 1);
            }

            SqlInjectionUtil.specialFilterContentForOnlineReport(var0);
            var0 = b(var0, var1);
            var0 = a(var0, "\\$");
            return var0;
        } else {
            throw new JimuReportException("sql为空");
        }
    }

    public static String a(String var0, String var1) {
        var0 = var0.replaceAll("(?i) where ", " where ");
        var0 = var0.replaceAll("(?i) and ", " and ");
        var0 = var0.replaceAll("(?i) or ", " or ");
        String var2 = "(,\\s*|\\s*(\\w|\\.)+\\s*[^, ]+ *\\S*)" + var1 + "\\{\\w+\\}\\S*";
        Pattern var3 = Pattern.compile(var2);

        for(Matcher var4 = var3.matcher(var0); var4.find(); e.debug("${}替换后结果 ==>" + var0)) {
            String var5 = var4.group();
            e.debug("${}匹配带参SQL片段 ==>" + var5);
            if (var5.indexOf(" where ") != -1) {
                String var6 = var5.substring(0, var5.indexOf(" where "));
                var0 = var0.replace(var5, var6 + " where 1=1");
            } else if (var5.indexOf(" and ") != -1) {
                var5 = var5.substring(var5.indexOf("and"));
                if (var5.indexOf("(") > 0) {
                    var5 = var5.substring(var5.indexOf("(") + 1);
                    var0 = var0.replace(var5, " 1=1 ");
                } else {
                    var0 = var0.replace(var5, "and 1=1");
                }
            } else if (var5.indexOf(" or ") != -1) {
                var5 = var5.substring(var5.indexOf("or"));
                if (var5.indexOf("(") > 0) {
                    var5 = var5.substring(var5.indexOf("(") + 1);
                    var0 = var0.replace(var5, " 1=1 ");
                } else {
                    var0 = var0.replace(var5, "or 1=1");
                }
            } else if (var5.startsWith(",")) {
                var0 = var0.replace(var5, " ,1 ");
            } else {
                var0 = var0.replace(var5, " 1=1 ");
            }
        }

        var0 = var0.replaceAll("(?i)\\(\\s*1=1\\s*(AND|OR)", "(");
        var0 = var0.replaceAll("(?i)(AND|OR)\\s*1=1", "");
        return var0;
    }

    public static String b(String var0, Map<String, Object> var1) {
        if (var1 != null) {
            Set var2 = a(var0);
            Iterator var3 = var2.iterator();

            while(var3.hasNext()) {
                String var4 = (String)var3.next();
                if (var1.get(var4) != null) {
                    String var5 = var1.get(var4).toString();
                    var0 = var0.replace("#{" + var4 + "}", var5);
                }
            }
        } else {
            var0 = a(var0, "\\#");
        }

        return var0;
    }

    public static Set<String> a(String var0) {
        if (oConvertUtils.isEmpty(var0)) {
            return null;
        } else {
            HashSet var1 = new HashSet();
            String var2 = "\\#\\{\\w+\\}";
            Pattern var3 = Pattern.compile(var2);
            Matcher var4 = var3.matcher(var0);

            while(var4.find()) {
                String var5 = var4.group();
                var1.add(var5.substring(var5.indexOf("{") + 1, var5.indexOf("}")));
            }

            return var1;
        }
    }

    public static List<String> b(String var0) {
        if (oConvertUtils.isEmpty(var0)) {
            return null;
        } else {
            ArrayList var1 = new ArrayList();
            String var2 = "\\$\\{\\w+\\}";
            Pattern var3 = Pattern.compile(var2);
            Matcher var4 = var3.matcher(var0);

            while(var4.find()) {
                String var5 = var4.group();
                var1.add(var5.substring(var5.indexOf("{") + 1, var5.indexOf("}")));
            }

            return var1;
        }
    }

    public static String c(String var0, Map<String, Object> var1) {
        String var2 = "\\{\\{\\s*domainURL\\s*\\}\\}";
        Pattern var3 = Pattern.compile(var2);
        Matcher var4 = var3.matcher(var0);
        if (var4.find()) {
            HttpServletRequest var5 = JimuSpringContextUtils.getHttpServletRequest();
            String var6 = a(var5);
            var0 = var0.replaceAll(var2, var6);
        }

        var0 = b(var0, var1);
        return var0;
    }

    public static String a(HttpServletRequest var0) {
        String var1 = var0.getScheme();
        String var2 = var0.getServerName();
        int var3 = var0.getServerPort();
        String var4 = var0.getContextPath();
        return var1 + "://" + var2 + ":" + var3 + var4;
    }

    public static String b(String var0, String var1) {
        HttpHeaders var2 = a();
        if (var2 == null) {
            var2 = new HttpHeaders();
            if (f != null) {
                var1 = f.getToken();
            }

            var2.add("X-Access-Token", var1);
            var2.add("token", var1);
        }

        ResponseEntity var3 = com.fuxi.cloud.desreport.b.f.a(var0, var2, String.class, new Object[0]);
        return (String)var3.getBody();
    }

    public static String a(String var0, String var1, Map<String, Object> var2) {
        HttpHeaders var3 = a();
        if (var3 == null) {
            var3 = new HttpHeaders();
            if (f != null) {
                var1 = f.getToken();
            }

            var3.add("X-Access-Token", var1);
            var3.add("token", var1);
        }

        ResponseEntity var4 = com.fuxi.cloud.desreport.b.f.a(var0, var3, var2, String.class, new Object[0]);
        return (String)var4.getBody();
    }

    public static String a(String var0, String var1, JSONObject var2) {
        HashMap var3 = new HashMap();
        if (var2 != null) {
            Iterator var4 = var2.entrySet().iterator();

            while(var4.hasNext()) {
                Map.Entry var5 = (Map.Entry)var4.next();
                var3.put(var5.getKey(), var5.getValue());
            }
        }

        return a((String)var0, (String)var1, (Map)var3);
    }

    public static void a(String var0, File var1) {
        ByteArrayInputStream var2 = null;
        FileOutputStream var3 = null;
        ResponseEntity var4 = com.fuxi.cloud.desreport.b.f.a(var0, byte[].class);
        byte[] var5 = (byte[])var4.getBody();

        try {
            var2 = new ByteArrayInputStream(var5);
            var3 = new FileOutputStream(var1);
            boolean var6 = false;
            byte[] var7 = new byte[1024];

            int var26;
            while((var26 = var2.read(var7, 0, 1024)) != -1) {
                var3.write(var7, 0, var26);
            }

            var3.flush();
        } catch (FileNotFoundException var23) {
            var23.printStackTrace();
        } catch (IOException var24) {
            var24.printStackTrace();
        } finally {
            if (var2 != null) {
                try {
                    var2.close();
                } catch (IOException var22) {
                    var22.printStackTrace();
                }
            }

            if (var3 != null) {
                try {
                    var3.close();
                } catch (IOException var21) {
                    var21.printStackTrace();
                }
            }

        }

    }

    private static String d(String var0) {
        if (var0.indexOf("token") < 0) {
            return null;
        } else {
            int var1 = var0.indexOf("?");
            if (var1 < 0) {
                return null;
            } else {
                String var2 = var0.substring(var1 + 1);
                String[] var3 = var2.split("&");
                String var4 = null;
                String[] var5 = var3;
                int var6 = var3.length;

                for(int var7 = 0; var7 < var6; ++var7) {
                    String var8 = var5[var7];
                    String[] var9 = var8.split("=");
                    if (var9[0].equals("token")) {
                        var4 = var9[1];
                        break;
                    }
                }

                return var4;
            }
        }
    }

    public static Object c(String var0, String var1) {
        if (oConvertUtils.isNotEmpty(var1)) {
            Object var2 = null;
            if ("java-class".equals(var0)) {
                try {
                    var2 = c(var1).newInstance();
                } catch (InstantiationException var4) {
                    e.error(var4.getMessage(), var4);
                } catch (IllegalAccessException var5) {
                    e.error(var5.getMessage(), var5);
                }
            } else if ("spring-key".equals(var0)) {
                var2 = JimuSpringContextUtils.getBean(var1);
            }

            return var2;
        } else {
            return null;
        }
    }

    public static Class c(String var0) {
        Class var1 = null;

        try {
            var1 = Class.forName(var0);
            return var1;
        } catch (ClassNotFoundException var3) {
            var3.printStackTrace();
            throw new RuntimeException(var0 + " not found!");
        }
    }

    public static IDataSetFactory d(String var0, String var1) {
        Object var2 = c(var0, var1);
        if (var2 != null && var2 instanceof IDataSetFactory) {
            IDataSetFactory var3 = (IDataSetFactory)var2;
            return var3;
        } else {
            return null;
        }
    }

    public static List<Map<String, Object>> a(IDataSetFactory var0, Map<String, Object> var1, Boolean var2) {
        List var3;
        if (var2) {
            JmPage var4 = var0.createPageData(var1);
            var3 = var4.getRecords();
        } else {
            var3 = var0.createData(var1);
        }

        ArrayList var11 = null;
        if (var3 != null && var3.size() > 0) {
            var11 = new ArrayList();
            Map var5 = (Map)var3.get(0);
            int var6 = 1;
            Iterator var7 = var5.entrySet().iterator();

            while(var7.hasNext()) {
                Map.Entry var8 = (Map.Entry)var7.next();
                String var9 = ((String)var8.getKey()).toLowerCase();
                LinkedHashMap var10 = new LinkedHashMap();
                var10.put("fieldName", var9);
                var10.put("fieldText", var9);
                var10.put("widgetType", "String");
                var10.put("orderNum", var6++);
                var11.add(var10);
            }
        }

        return var11;
    }

    public static Map<String, Object> a(JSONObject var0) {
        HashMap var1 = new HashMap();
        Iterator var2 = var0.entrySet().iterator();

        while(var2.hasNext()) {
            Map.Entry var3 = (Map.Entry)var2.next();
            var1.put(var3.getKey(), var3.getValue());
        }

        return var1;
    }

    private static HttpHeaders a() {
        HttpHeaders var0 = null;
        if (f == null) {
            f = (JmReportTokenClient)JimuSpringContextUtils.getBean(JmReportTokenClient.class);
        }

        if (f != null) {
            var0 = f.customApiHeader();
        }

        return var0;
    }
}
